
package system.base;

import framework.core.*;
import system.data.*;
/*
	Author    :Wing
	CreateTime:2008-02-04 10:56:45
*/
public class FunctionSrv extends IHandler
{

	public FunctionSrv() 
	{
		super();
	}
	private void deleteFunction(DBConnection con,SrvContext context) throws Exception
	{
		String[] ids = context.getParameterValues("ids");
		Function obj = null;
		for(int i=0;i<ids.length;i++)
		{
			obj = Function.fetch(con,ids[i]);
			if(obj!=null) obj.delete(con);
		}
	}
	private void saveFunction(DBConnection con,SrvContext context,ILoginContext loginContext) throws Exception
	{
		String id = context.getString("Id",null);
		Function obj;
		if((id==null)||(id.equals("")))   //add
		{
			obj = Function.fetch();
    	}
    	else                     //update
    	{
    		obj = Function.fetch(con,id);
    	}
		FunctionPageData data = (FunctionPageData)PageData.fromForm(context,FunctionPageData.class);
		context.setValue("FunctionPageData",data);
		obj.fill(data);
		obj.save(con);
	}
	private void listFunction(DBConnection con,SrvContext context,String path) throws Exception
	{
		FunctionSearchEvent event = (FunctionSearchEvent)SearchEvent.fromForm(context,FunctionSearchEvent.class);
		context.setSessionValue("FunctionSearchEvent",event);
		context.setNextUrl(path+"FunctionList.jsp");
	}
	private void backFunction(DBConnection con,SrvContext context,String path) throws Exception
	{
		context.setNextUrl(path+"FunctionList.jsp");
	}
	
	public void doAction(DBConnection con,SrvContext context,ILoginContext loginContext) throws Exception
	{
		String action_code = context.getActionCode();
		String path = "/system/base/";
		
		if(action_code.equalsIgnoreCase("listFunction"))
		{
			listFunction(con,context,path);
			return;
		}
		if(action_code.equalsIgnoreCase("viewFunction"))
		{
			String id = context.getString("Id",null);
			Function obj = Function.fetch(con,id);
			context.setValue("Function",obj);
			context.setNextUrl(path+"FunctionView.jsp");
			return;
		}
		if(action_code.equalsIgnoreCase("deleteFunction"))
		{
			context.setErrorUrl(path+"FunctionList.jsp");
			deleteFunction(con,context);
			backFunction(con,context,path);
			return;
		}
			
		if(action_code.equalsIgnoreCase("toAddFunction"))
		{
			Function obj = Function.fetch();
			FunctionPageData data = (FunctionPageData)PageData.fromTable(obj,FunctionPageData.class);
			context.setValue("FunctionPageData",data);
			context.setNextUrl(path+"FunctionEdit.jsp");
			return;
		}
		if(action_code.equalsIgnoreCase("toUpdateFunction"))
		{
			String id = context.getString("Id",null);
			Function obj = Function.fetch(con,id);
			FunctionPageData data = (FunctionPageData)PageData.fromTable(obj,FunctionPageData.class);
			context.setValue("FunctionPageData",data);
			context.setNextUrl(path+"FunctionEdit.jsp");
			return;
		}
		if(action_code.equalsIgnoreCase("toCopyFunction"))
		{
			String id = context.getString("Id",null);
			Function obj = Function.copy(Function.fetch(con,id));
			FunctionPageData data = (FunctionPageData)PageData.fromTable(obj,FunctionPageData.class);
			context.setValue("FunctionPageData",data);
			context.setNextUrl(path+"FunctionEdit.jsp");
			return;
		}
		if(action_code.equalsIgnoreCase("saveFunction"))
		{
			context.setErrorUrl(path+"FunctionEdit.jsp");
			saveFunction(con,context,loginContext);
			backFunction(con,context,path);
			return;
		}
		if(action_code.equalsIgnoreCase("saveAndNewFunction"))
		{
			context.setErrorUrl(path+"FunctionEdit.jsp");
			saveFunction(con,context,loginContext);
			Function obj = Function.fetch();
			FunctionPageData data = (FunctionPageData)PageData.fromTable(obj,FunctionPageData.class);
			context.setValue("FunctionPageData",data);
			context.setNextUrl(path+"FunctionEdit.jsp");			
			return;
		}
		if(action_code.equalsIgnoreCase("backListFunction"))
		{
			backFunction(con,context,path);
			return;
		}
		
		//add by Wing start

		//add by Wing end

		Logger.throwException("FunctionSrv","unknown action_code="+action_code);
	}
}
